{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 读写文件（第8章）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**1.1 文件与文件路径**  \n",
    "通过import os调用os模块操作目录，常用函数如下："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|函数|用途|\n",
    "|-|:-|\n",
    "|os.getcwd()|取得当前工作路径|\n",
    "|os.chdir()|改变当前工作路径|\n",
    "|os.makedirs(path)|创建新文件夹|\n",
    "|os.listdir(path)|返回文件名字符串的列表|\n",
    "|os.path.join(path1,path2,...)|连接路径|\n",
    "|os.path.abspath(path)|返回绝对路径|\n",
    "|os.path.isabs(path)|检查是否绝对路径|\n",
    "|os.path.relpath(path, start)|返回从start路径到path的相对路径|\n",
    "|os.path.dirname(path)|返回目录名|\n",
    "|os.path.basename(path)|返回文件名|\n",
    "|os.path.split(path)|返回目录名和文件名的元组|\n",
    "|os.path.getsize(path)|返回path参数中文件的字节数|\n",
    "|os.path.exists(path)|检查路径是否存在|\n",
    "|os.path.isfile(path)|检查路径是否文件|\n",
    "|os.path.isdir(path)|检查路径是否文件夹|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**1.2 文件读写**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在Python 中，读写文件有3 个步骤：\n",
    "1. 调用open()函数，返回一个File 对象。\n",
    "2. 调用File 对象的read()或write()方法。\n",
    "3. 调用File 对象的close()方法，关闭该文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world!\n",
      "Bacon is not a vegetable.\n"
     ]
    }
   ],
   "source": [
    "# 将'w'作为第二个参数传递给open(),以写模式打开该文件\n",
    "# 写模式会清空源文件\n",
    "baconFile = open('bacon.txt', 'w')\n",
    "baconFile.write('Hello world!\\n')\n",
    "baconFile.close()\n",
    "\n",
    "# 将'a'作为第二个参数传递给open()，以添加模式打开该文件\n",
    "# 添加模式将在已有文件的末尾添加文本\n",
    "baconFile = open('bacon.txt', 'a')\n",
    "baconFile.write('Bacon is not a vegetable.')\n",
    "baconFile.close()\n",
    "\n",
    "# 读文件\n",
    "baconFile = open('bacon.txt')\n",
    "# read将文件的内容看成是单个大字符串\n",
    "content = baconFile.read()\n",
    "# readlines从该文件取得一个字符串的列表。列表中的每个字符串就是文本中的每一行\n",
    "# content = baconFile.readlines()\n",
    "baconFile.close()\n",
    "print(content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 项目练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1 生成随机的测验试卷文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "import random\n",
    "\n",
    "\n",
    "# 测验数据\n",
    "# The quiz data. Keys are states and values are their capitals.\n",
    "capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', 'Arizona': 'Phoenix',\n",
    "'Arkansas': 'Little Rock', 'California': 'Sacramento', 'Colorado': 'Denver',\n",
    "'Connecticut': 'Hartford', 'Delaware': 'Dover', 'Florida': 'Tallahassee',\n",
    "'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', 'Idaho': 'Boise', 'Illinois':\n",
    "'Springfield', 'Indiana': 'Indianapolis', 'Iowa': 'Des Moines', 'Kansas':\n",
    "'Topeka', 'Kentucky': 'Frankfort', 'Louisiana': 'Baton Rouge', 'Maine':\n",
    "'Augusta', 'Maryland': 'Annapolis', 'Massachusetts': 'Boston', 'Michigan':\n",
    "'Lansing', 'Minnesota': 'Saint Paul', 'Mississippi': 'Jackson', 'Missouri':\n",
    "'Jefferson City', 'Montana': 'Helena', 'Nebraska': 'Lincoln', 'Nevada':\n",
    "'Carson City', 'New Hampshire': 'Concord', 'New Jersey': 'Trenton', 'New Mexico': 'Santa Fe', 'New York': 'Albany', 'North Carolina': 'Raleigh',\n",
    "'North Dakota': 'Bismarck', 'Ohio': 'Columbus', 'Oklahoma': 'Oklahoma City',\n",
    "'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', 'Rhode Island': 'Providence',\n",
    "'South Carolina': 'Columbia', 'South Dakota': 'Pierre', 'Tennessee':\n",
    "'Nashville', 'Texas': 'Austin', 'Utah': 'Salt Lake City', 'Vermont':\n",
    "'Montpelier', 'Virginia': 'Richmond', 'Washington': 'Olympia', \n",
    "'West Virginia': 'Charleston', 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'}\n",
    "    \n",
    "    \n",
    "# 生成试卷\n",
    "# 试卷份数    \n",
    "n_quiz=2\n",
    "for quizNum in range(n_quiz):\n",
    "    # Create the quiz and answer key files.\n",
    "    # 试卷\n",
    "    quizFile = open('capitalsquiz%s.txt' % (quizNum + 1), 'w')\n",
    "    # 答案\n",
    "    answerKeyFile = open('capitalsquiz_answers%s.txt' % (quizNum + 1), 'w')\n",
    "    # Write out the header for the quiz.\n",
    "    # 头文件\n",
    "    quizFile.write('Name:\\n\\nDate:\\n\\nPeriod:\\n\\n')\n",
    "    quizFile.write((' ' * 20) + 'State Capitals Quiz (Form %s)' % (quizNum + 1))\n",
    "    quizFile.write('\\n\\n')\n",
    "    # Shuffle the order of the states.\n",
    "    states = list(capitals.keys())\n",
    "    # 创建了美国州名的随机列表\n",
    "    random.shuffle(states)    \n",
    "    \n",
    "    \n",
    "    # 创建答案    \n",
    "    # Loop through all 50 states, making a question for each.\n",
    "    for questionNum in range(50):\n",
    "        # Get right and wrong answers.\n",
    "        # 正确答案\n",
    "        correctAnswer = capitals[states[questionNum]]\n",
    "        # 错误答案\n",
    "        wrongAnswers = list(capitals.values())\n",
    "        # 删除正确的答案\n",
    "        del wrongAnswers[wrongAnswers.index(correctAnswer)]\n",
    "        # 随机取出三个答案\n",
    "        wrongAnswers = random.sample(wrongAnswers, 3)\n",
    "        answerOptions = wrongAnswers + [correctAnswer]\n",
    "        random.shuffle(answerOptions)    \n",
    "    \n",
    "        # 将内容写入测验试卷和答案文件\n",
    "        # Write the question and the answer options to the quiz file.\n",
    "        quizFile.write('%s. What is the capital of %s?\\n' % (questionNum + 1,states[questionNum]))\n",
    "        \n",
    "    \n",
    "        for i in range(4):\n",
    "            quizFile.write(' %s. %s\\n' % ('ABCD'[i], answerOptions[i]))\n",
    "        quizFile.write('\\n')\n",
    "        \n",
    "        # Write the answer key to a file.   \n",
    "        answerKeyFile.write('%s. %s\\n' % (questionNum + 1, 'ABCD'[answerOptions.index(correctAnswer)]))\n",
    "    quizFile.close()\n",
    "    answerKeyFile.close()    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2 疯狂填词\n",
    "创建一个疯狂填词（Mad Libs）程序，它将读入文本文件，并让用户在该文本文件中出现ADJECTIVE、NOUN、ADVERB 或VERB 等单词的地方，加上他们自己的文本。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter an ADJECTIVE:\n",
      "silly\n",
      "Enter a NOUN:\n",
      "chandelier\n",
      "Enter a VERB:\n",
      "screamed\n",
      "Enter a NOUN:\n",
      "pickup truck\n",
      "The silly panda walked to the chandelier and then screamed. A nearby pickup truck was unaffected by these events.\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "# text:\n",
    "'''\n",
    "The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events\n",
    "'''\n",
    "# 输入文件\n",
    "input_file = open('./input_text.txt')\n",
    "input_text = input_file.read()\n",
    "input_file.close()\n",
    "\n",
    "# 输出文本\n",
    "output_text = input_text\n",
    "\n",
    "# 输入文本寻找关键词\n",
    "keywords = re.compile('adjective|noun|verb', re.IGNORECASE)\n",
    "mo = keywords.findall(input_text)\n",
    "\n",
    "# 一个一个词进行替换\n",
    "for word in mo:\n",
    "    if word[0].lower() == 'a':\n",
    "        replacewords = input('Enter an {}:\\n'.format(word))\n",
    "    else:\n",
    "        replacewords = input('Enter a {}:\\n'.format(word))\n",
    "    regex = re.compile(word)\n",
    "    # 每一次替换一个词\n",
    "    output_text = regex.sub(replacewords, output_text, 1)\n",
    "\n",
    "# 写入新文件\n",
    "output_file = open('output_text.txt', 'w')\n",
    "output_file.write(output_text)\n",
    "output_file.close()\n",
    "print(output_text)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.3 正则表达式查找\n",
    "编写一个程序，打开文件夹中所有的.txt 文件，查找匹配用户提供的正则表达式的所有行。结果应该打印到屏幕上。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events.\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import os\n",
    "import re\n",
    "\n",
    "path = \"./\"\n",
    "\n",
    "# 判断路径目录是否存在\n",
    "folder = os.path.exists(path)\n",
    "if folder == False:\n",
    "    print(\"文件目录不存在！\")\n",
    "\n",
    "\n",
    "#返回指定目录下所有的文件名和文件夹名列表\n",
    "file_Name = os.listdir(path)\n",
    "\n",
    "#创建正则表达式\n",
    "keywords = re.compile(r'ADJECTIVE')\n",
    "\n",
    "# 找出txt文件\n",
    "for i in range(len(file_Name)):\n",
    "    #判断是否为txt文件\n",
    "    if os.path.isfile(os.path.join(path, file_Name[i])) and file_Name[i].split('.')[-1] == 'txt':\n",
    "        # 打开文件\n",
    "        file = open(os.path.join(path, file_Name[i]))\n",
    "        file_texts = file.readlines()\n",
    "        for texts in file_texts:\n",
    "            #找出匹配正则表达式的内容\n",
    "            text_put = keywords.search(texts)\n",
    "            #输出结果\n",
    "            if text_put != None:\n",
    "                #打印当前行\n",
    "                print('{}'.format(texts))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
